#!/usr/bin/env python
# coding=utf-8
import datetime

from django.db import models
from django.utils import timezone

# Create your models here.

class Question(models.Model):

    content = models.CharField(max_length = 200)
    publish_date = models.DateTimeField('published date')

    def __str__(self):
        return self.content


    def is_published_recently(self):
        "check if question published in recent 1 day"
        now = timezone.now()
        return now > self.publish_date >= now - datetime.timedelta(days=1)

    # 客制化该字段在admin管理视图表单中的显示
    is_published_recently.admin_order_field = 'publish_date'
    is_published_recently.boolean = True
    is_published_recently.short_description = "Published recently?"



class Choice(models.Model):

    question = models.ForeignKey(Question, on_delete=models.CASCADE)

    choice_content = models.CharField(max_length = 200)

    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_content


# The project object which represent a project
class Project(models.Model):

    PROJECT_STATUS = (
        (None, 'Select...'),
        ('ST', 'Started'),
        ('AS', 'Assigned'),
        ('CM', 'Committed'),
        ('TS', 'Tested'),
        ('FN', 'Finished'),
    )
    name = models.CharField(max_length = 200)
    status = models.CharField(max_length = 10, choices=PROJECT_STATUS)
    create_date = models.DateTimeField("Creation Date", help_text="Please following the format:<em>YYYY-MM-DD</em>")
    finish_date = models.DateTimeField("End Date")
    finished=models.BooleanField("Finished Project",default=True)
    attachment = models.FileField(upload_to='projects/%Y/%m/%d', default=None)
    filepath = models.FilePathField(path="/home/liang/projects/django_showcase/uploaded/",  default="./")
    files = models.CharField(null=True, max_length=100, default=None)


class Test(models.Model):
    str_field = models.CharField(max_length=50)
    int_field = models.IntegerField()
    date_field = models.DateField()

class Address(models.Model):

    number = models.IntegerField()
